package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.admin.dao.RoleMenuDao;
import com.wei.czz.framework.admin.entity.RoleMenuEntity;
import com.wei.czz.framework.admin.service.RoleMenuService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-03-15 10:30:05
 * className: RoleMenuServiceImpl 角色和菜单关联操作请求服务接口实现类
 * version: 1.1
 * description:
 */
@Service("roleMenuService")
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenuEntity> implements RoleMenuService {

    private static final Logger log = LoggerFactory.getLogger(RoleMenuService.class);

    @Transactional
    @Override
    public void batchSave(List<RoleMenuEntity> roleMenuList) {
        boolean bool = this.saveBatch(roleMenuList);
        log.info("批量保存角色关联菜单完成。size={} bool={}", roleMenuList.size(), bool);
    }

    @Override
    public List<String> getRoleMenuIdList(Long roleId) {
        return baseMapper.getMenuListByRoleId(roleId, CommonEnum.ZERO.getValue());
    }

    @Override
    public List<Long> findMenuListByRoleId(Long roleId) {
        return baseMapper.findMenuListByRoleId(roleId, CommonEnum.ZERO.getValue());
    }

    @Override
    public List<Long> findRoleIdList(Long menuId) {
        return baseMapper.findRoleIdList(menuId, CommonEnum.ZERO.getValue());
    }

    @Override
    public List<RoleMenuEntity> findList(Long roleId, Long menuId, Integer deleteStatus) {
        if (ObjectUtils.allNull(roleId, menuId)) {
            log.info("获取角色与菜单关联数据，传递的角色主键和菜单主键为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleMenuEntity> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(Objects.nonNull(roleId), RoleMenuEntity::getRoleId, roleId)
                .eq(Objects.nonNull(menuId), RoleMenuEntity::getMenuId, menuId)
                .eq(Objects.nonNull(deleteStatus), RoleMenuEntity::getDeleteStatus, deleteStatus);
        // 查询
        List<RoleMenuEntity> roleMenuList = baseMapper.selectList(roleMenuLambdaQueryWrapper);
        log.info("查询角色与菜单关联数据完成。roleId={} menuId={} deleteStatus={}", roleId, menuId, deleteStatus);
        return roleMenuList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<RoleMenuEntity> roleMenuList) {
        boolean bool = this.updateBatchById(roleMenuList);
        log.info("批量修改角色关联菜单完成。size={} bool={}", roleMenuList.size(), bool);
    }

    @Override
    public void delete(List<Long> roleIdList, List<Long> menuIdList) {
        if (CollectionUtils.isEmpty(roleIdList) && CollectionUtils.isEmpty(menuIdList)) {
            log.info("批量删除角色关联菜单，传入的角色主键列表和菜单主键列表均为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<RoleMenuEntity> roleMenuLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roleMenuLambdaUpdateWrapper.set(RoleMenuEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(RoleMenuEntity::getUpdateTime, date)
                .set(RoleMenuEntity::getUpdateUser, userPo.getUsername())
                .set(RoleMenuEntity::getUpdateUserId, userPo.getUserId());
        roleMenuLambdaUpdateWrapper.in(!CollectionUtils.isEmpty(roleIdList), RoleMenuEntity::getRoleId, roleIdList)
                .in(!CollectionUtils.isEmpty(menuIdList), RoleMenuEntity::getMenuId, menuIdList)
                .eq(RoleMenuEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(roleMenuLambdaUpdateWrapper);
        log.info("批量删除角色关联菜单完成。count={}", count);
    }
}